#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5+5;
int w[maxn];
int l[maxn], r[maxn];
int e[maxn];
int ans;
void dfs(int x){
	if (e[x] == 0) {
		ans += w[x];
	}
	if (l[x] != -1) {
		e[l[x]] = e[x] + 1;
		dfs(l[x]);
	}
	
	if (r[x] != -1) {
		e[r[x]] = e[x] - 1;
		dfs(r[x]);
	}
}

void solve(){
	int n;
	cin >> n;
	for (int i=1;i<=n;i++) {
		cin >> w[i];
	}
	
	for (int i=1;i<=n;i++) {
		cin >> l[i] >> r[i];
	}
	
	dfs(1);
	cout << ans << '\n';
}


signed main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}
